package com.zbkj.admin.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.github.pagehelper.PageInfo;
import com.zbkj.admin.exception.CrmebException;
import com.zbkj.admin.model.admin.SystemAdmin;
import com.zbkj.admin.model.product.*;
import com.zbkj.admin.result.CommonResultCode;
import com.zbkj.admin.service.*;
import com.zbkj.admin.util.SecurityUtil;
import com.zbkj.admin.util.yly.Utils;
import com.zbkj.admin.vo.LoginUserVo;
import com.zbkj.admin.vo.MyRecord;
import com.zbkj.admin.request.*;
import com.zbkj.admin.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单管理 service 调用业务
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class ProductManagerServiceImpl implements ProductManagerService {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    @Autowired
    private ProductAttributeService productAttributeService;
    @Autowired
    private ProductAttributeOptionService productAttributeOptionService;
    @Autowired
    private ProductDescriptionService productDescriptionService;

    /**
     * 获取产品列表Admin
     *
     * @param request 筛选参数
     * @return PageInfo
     */
    @Override
    public PageInfo<AdminProductListResponse> getAdminList(MerProductSearchRequest request) {
        LoginUserVo loginUserVo = SecurityUtil.getLoginUserVo();
        SystemAdmin admin = null;
        if (ObjUtil.isNotEmpty(loginUserVo)) {
            admin = SecurityUtil.getLoginUserVo().getUser();
        }
        return productService.getAdminList(request, admin);
    }

    /**
     * 新增产品
     *
     * @param request 新增产品request对象
     * @return 新增结果
     */
    @Override
    public Boolean save(ProductAddRequest request) {
        return productService.save(request);
    }


    /**
     * 更新商品信息
     *
     * @param productRequest 商品参数
     * @return 更新结果
     */
    @Override
    public Boolean update(ProductAddRequest productRequest) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.update(productRequest, admin);
    }

    /**
     * 商品详情（管理端）
     *
     * @param id 商品id
     * @return ProductInfoResponse
     */
    @Override
    public ProductInfoResponse getInfo(Integer id) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.getInfo(id, admin);
    }

    /**
     * 根据商品tabs获取对应类型的产品数量
     *
     * @return List
     */
    @Override
    public List<ProductTabsHeaderResponse> getTabsHeader(MerProductTabsHeaderRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.getTabsHeader(request, admin);
    }

    /**
     * 根据其他平台url导入产品信息
     *
     * @param url 待导入平台url
     * @param tag 1=淘宝，2=京东，3=苏宁，4=拼多多， 5=天猫
     * @return ProductRequest
     */
    @Override
    public ProductResponseForCopyProduct importProductFrom99Api(String url, int tag) throws JSONException, IOException {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.importProductFrom99Api(url, tag, admin);
    }

    /**
     * 商品回收/删除
     *
     * @param request 删除参数
     * @return Boolean
     */
    @Override
    public Boolean deleteProduct(ProductDeleteRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.deleteProduct(request, admin);
    }

    /**
     * 恢复已删除的商品
     *
     * @param productId 商品id
     * @return 恢复结果
     */
    @Override
    public Boolean restoreProduct(Integer productId) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.restoreProduct(productId, admin);
    }

    /**
     * 下架
     *
     * @param id 商品id
     */
    @Override
    public Boolean offShelf(Integer id) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.offShelf(id, admin);
    }

    /**
     * 上架
     *
     * @param id 商品id
     * @return Boolean
     */
    @Override
    public Boolean putOnShelf(Integer id) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.putOnShelf(id, admin);
    }

    /**
     * 平台端商品分页列表
     *
     * @param request 查询参数
     * @return PageInfo
     */
    @Override
    public PageInfo<PlatformProductListResponse> getPlatformPageList(PlatProductSearchRequest request) {
        return productService.getPlatformPageList(request);
    }

    /**
     * 根据id集合以及活动上限加载商品数据
     *
     * @param ids id集合
     * @return 平台商品列表
     */
    @Override
    public List<PlatformProductListResponse> getPlatformListForIdsByLimit(List<Integer> ids) {
        return productService.getPlatformListForIdsByLimit(ids);
    }

    /**
     * 商品审核
     *
     * @param request 审核参数
     * @return Boolean
     */
    @Override
    public Boolean audit(ProductAuditRequest request) {
        return productService.audit(request);
    }

    /**
     * 强制下架商品
     *
     * @param request 商品id参数
     * @return Boolean
     */
    @Override
    public Boolean forceDown(ProductForceDownRequest request) {
        return productService.forceDown(request);
    }

    /**
     * 平台端获取商品表头数量
     *
     * @return List
     */
    @Override
    public List<ProductTabsHeaderResponse> getPlatformTabsHeader(PlatProductTabsHeaderRequest request) {
        return productService.getPlatformTabsHeader(request);
    }

    /**
     * 平台端商品编辑
     *
     * @param request 商品编辑参数
     * @return Boolean
     */
    @Override
    public Boolean platUpdate(ProductPlatUpdateRequest request) {
        return productService.platUpdate(request);
    }

    /**
     * 商品提审
     */
    @Override
    public Boolean submitAudit(ProductSubmitAuditRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.submitAudit(request, admin);
    }

    /**
     * 快捷添加库存
     *
     * @param request 添加库存参数
     * @return Boolean
     */
    @Override
    public Boolean quickAddStock(ProductAddStockRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.quickAddStock(request, admin);
    }

    /**
     * 商品免审编辑
     *
     * @param request 商品免审编辑参数
     * @return Boolean
     */
    @Override
    public Boolean reviewFreeEdit(ProductReviewFreeEditRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.reviewFreeEdit(request, admin);
    }

    /**
     * 获取复制商品配置
     *
     * @return copyType 复制类型：1：一号通
     * copyNum 复制条数(一号通类型下有值)
     */
    @Override
    public MyRecord copyConfig() {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.copyConfig(admin);
    }

    /**
     * 复制平台商品
     *
     * @param url 商品链接
     * @return MyRecord
     */
    @Override
    public ProductResponseForCopyProduct copyProduct(String url) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.copyProduct(url, admin);
    }

    /**
     * 商品搜索分页列表（活动）
     *
     * @param request 搜索参数
     * @return PageInfo
     */
    @Override
    public PageInfo<ProductActivityResponse> getActivitySearchPage(ProductActivitySearchRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.getActivitySearchPage(request, admin);
    }

    /**
     * 商品搜索分页列表（营销）
     *
     * @param request 搜索参数
     * @return PageInfo
     */
    @Override
    public PageInfo<ProductMarketingResponse> getMarketingSearchPage(PlatProductMarketingSearchRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.getMarketingSearchPage(request, admin);
    }

    /**
     * 商品搜索分页列表（活动）商户端
     *
     * @param request 搜索参数
     * @return PageInfo
     */
    @Override
    public PageInfo<ProductActivityResponse> getActivitySearchPageByMerchant(ProductActivitySearchRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.getActivitySearchPageByMerchant(request, admin);
    }

    /**
     * 商品搜索分页列表（营销）商户端
     *
     * @param request 搜索参数
     * @return PageInfo
     */
    @Override
    public PageInfo<ProductMarketingResponse> getMarketingSearchPageByMerchant(MerProductMarketingSearchRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.getMarketingSearchPageByMerchant(request, admin);
    }

    /**
     * 设置运费模板
     */
    @Override
    public Boolean setFreightTemplate(ProductFreightTemplateRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.setFreightTemplate(request, admin);
    }

    /**
     * 设置佣金
     */
    @Override
    public Boolean setBrokerage(ProductSetBrokerageRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.setBrokerage(request, admin);
    }

    /**
     * 添加回馈券
     */
    @Override
    public Boolean addFeedbackCoupons(ProductAddFeedbackCouponsRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.addFeedbackCoupons(request, admin);
    }

    /**
     * 批量上架商品
     *
     * @param idList 商品ID列表
     */
    @Override
    public Boolean batchUp(List<Integer> idList) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.batchUp(idList, admin);
    }

    /**
     * 批量商品下架
     *
     * @param idList 商品ID列表
     */
    @Override
    public Boolean batchDown(List<Integer> idList) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.batchDown(idList, admin);
    }

    /**
     * 批量设置运费模板
     */
    @Override
    public Boolean batchSetFreightTemplate(BatchSetProductFreightTemplateRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.batchSetFreightTemplate(request, admin);
    }

    /**
     * 批量设置佣金
     */
    @Override
    public Boolean batchSetBrokerage(BatchSetProductBrokerageRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.batchSetBrokerage(request, admin);
    }

    /**
     * 批量添加回馈券
     */
    @Override
    public Boolean batchAddFeedbackCoupons(BatchAddProductFeedbackCouponsRequest request) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.batchAddFeedbackCoupons(request, admin);
    }

    /**
     * 商品批量加入回收站
     *
     * @param idList 商品ID列表
     */
    @Override
    public Boolean batchRecycle(List<Integer> idList) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.batchRecycle(idList, admin);
    }

    /**
     * 批量删除商品
     *
     * @param idList 商品ID列表
     */
    @Override
    public Boolean batchDelete(List<Integer> idList) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.batchDelete(idList, admin);
    }

    /**
     * 批量恢复回收站商品
     *
     * @param idList 商品ID列表
     */
    @Override
    public Boolean batchRestore(List<Integer> idList) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.batchRestore(idList, admin);
    }

    /**
     * 批量提审商品
     *
     * @param idList 商品ID列表
     */
    @Override
    public Boolean batchSubmitAudit(List<Integer> idList) {
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();
        return productService.batchSubmitAudit(idList, admin);
    }

    /**
     * 平台端批量设置虚拟销量
     */
    @Override
    public Boolean platBatchSetVirtualSales(BatchSetVirtualSalesRequest request) {
        return productService.platBatchSetVirtualSales(request);
    }

    /**
     * 平台端批量商品审核
     */
    @Override
    public Boolean batchAudit(BatchProductAuditRequest request) {
        return productService.batchAudit(request);
    }

    /**
     * 同步金牛资源供销-商品信息
     * */
    @Override
    public Boolean syncDakeData(String mobile){
        SystemAdmin admin = SecurityUtil.getLoginUserVo().getUser();

        // 生成查询参数
        String timestamp = Utils.getTimestamp(); //   必需 时间戳秒级
        String jinniumiyao = "2kxlQB8eWv5SjyqaUDpkBFxiNGQEwlq6"; // 密钥
        // 生成sign
        String sign = Utils.getMD5Str(mobile + timestamp + jinniumiyao);; //  必需 签名： md5(手机号 + timestamp + 接口密钥)

        // 调用金牛接口 - 获取商品信息
        HttpResponse response = HttpRequest.get("https://w.lp97.com.cn/open/express/list")
                .header("Accept", "text/html;charset=UTF-8;")
                .form("mobile", mobile) // 使用form方法传递查询参数，等同于URL中的?key1=value1&key2=value2
                .form("timestamp", timestamp)
                .form("sign", sign)
                .execute(); // 执行请求

        // 检查响应状态码
        if (response.isOk()) {
            // 查询已对接商品数据id
            List<Integer> jinniuGoodIds = productService.findGoodIdByMerId(admin.getMerId());
            // 定义保存对象
            ArrayList<Product> productList = new ArrayList<>();
            // 接口调用成功、转换body数据JSON对象
            JSONObject bodyJson = JSONUtil.parseObj(response.body());
            System.out.println("对接商品数据:"+bodyJson);
            // 判断快递信息是否存在
            if(bodyJson != null && bodyJson.get("data") != null){
                List<Map<String, Object>> dataList = (List<Map<String, Object>>) bodyJson.get("data");
                // 循环获取 快递信息
                for(Map<String, Object> dataMap : dataList){
                    // 获取快递价格
                    BigDecimal kuaidiPrice = dataMap.get("price") != null ? new BigDecimal(dataMap.get("price").toString()) : new BigDecimal(0);
                    // 判断是否存在商品信息
                    if(dataMap.get("goods_list") != null){
                        List<Map<String, Object>> goodsList = (List<Map<String, Object>>) dataMap.get("goods_list");
                        // 循环获取 商品信息
                        for(Map<String, Object> goodMap : goodsList){
                            // 根据id 判断是否已保存数据
                            if(!jinniuGoodIds.contains(Integer.parseInt(goodMap.get("goods_id").toString()))){
                                jinniuGoodIds.add(Integer.parseInt(goodMap.get("goods_id").toString()));
                                // 判断信息是否已存在 新增数据
                                Product product = new Product();
                                product.setMerId(admin.getMerId()); // 商户id
                                product.setImage(goodMap.get("goods_pic").toString()); // 展示图片
                                product.setSliderImage("[\""+goodMap.get("goods_pic").toString()+"\"]");
                                product.setName(goodMap.get("goods_name").toString()); // 商品名称
                                product.setIntro(goodMap.get("goods_info").toString()); // 商品简介
                                product.setKeyword(goodMap.get("goods_info").toString()); // 关键字
                                product.setPrice(new BigDecimal(goodMap.get("price").toString()).add(kuaidiPrice)); // 商品价格
                                product.setCost(new BigDecimal(goodMap.get("price").toString()).add(kuaidiPrice)); // 市场价
                                product.setStock(Integer.parseInt(goodMap.get("sku_num").toString())); // 库存数量
                                product.setIsAudit(Boolean.TRUE);
                                product.setIsRecycle(Boolean.FALSE);
                                product.setSupplier("大客");
                                product.setGoodId(Integer.parseInt(goodMap.get("goods_id").toString())); // 保存关联对象
                                // 保存商品信息
                                productService.save(product);

                                // 生成商品关联数据
                                ProductAttrValue productAttrValue = new ProductAttrValue();
                                productAttrValue.setProductId(product.getId());
                                productAttrValue.setSku("默认");
                                productAttrValue.setStock(product.getStock());
                                productAttrValue.setPrice(product.getPrice());
                                productAttrValue.setCost(new BigDecimal(goodMap.get("price").toString()).add(kuaidiPrice));
                                productAttrValue.setOtPrice(new BigDecimal(goodMap.get("price").toString()));
                                productAttrValue.setImage(product.getImage());
                                productAttrValueService.save(productAttrValue);

                                // 生成商品规格信息
                                ProductAttribute productAttribute = new ProductAttribute();
                                productAttribute.setProductId(product.getId());
                                productAttribute.setAttributeName("规格");
                                productAttribute.setIsShowImage(Boolean.TRUE);
                                productAttribute.setSort(0);
                                productAttribute.setIsDel(Boolean.FALSE);
                                productAttributeService.save(productAttribute);

                                // 商品规格属性表
                                ProductAttributeOption productAttributeOption = new ProductAttributeOption();
                                productAttributeOption.setProductId(product.getId());
                                productAttributeOption.setAttributeId(productAttribute.getId());
                                productAttributeOption.setOptionName("默认");
                                productAttributeOption.setImage("");
                                productAttributeOption.setSort(1);
                                productAttributeOption.setIsDel(Boolean.FALSE);
                                productAttributeOptionService.save(productAttributeOption);

                                // 商品描述表
                                ProductDescription productDescription = new ProductDescription();
                                productDescription.setProductId(product.getId());
                                productDescription.setDescription(goodMap.get("goods_info").toString());
                                productDescription.setType(0);
                                productDescription.setMarketingType(0);
                                productDescriptionService.save(productDescription);

                            }
                        }
                    }
                }
            }
            return Boolean.TRUE;
        } else {
            throw new CrmebException(CommonResultCode.VALIDATE_FAILED, "请求失败，状态码：" + response.getStatus());
        }
    }
}

